//
//  PGTabBarViewController.swift
//  PGWeibo
//
//  Created by Apple on 15/11/3.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class PGTabBarViewController: UITabBarController {
    
    @objc private func click() {
        print("点了")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        //创建所有子控制器
        addChildViewControllers()
        //设置compose按钮
        setupComposeButton()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.bringSubviewToFront(composeButton)
    }
    
    private lazy var composeButton: UIButton = UIButton(iconImage: "tabbar_compose_icon_add", BackImage: "tabbar_compose_button", title: nil)
    
}
// MARK: - 创建子控制器
extension PGTabBarViewController {
    
    func setupComposeButton() {
        //添加compose按钮
        tabBar.addSubview(composeButton)
        
//        //调整位置
        let count = childViewControllers.count
        let W = tabBar.bounds.width / CGFloat (count) - 1
        composeButton.frame = CGRectInset(tabBar.bounds, 2 * W, 0)
        
        composeButton.addTarget(self, action: "click", forControlEvents: .TouchUpInside)
    }
    
    
    /// 创建所有子控制器
    func addChildViewControllers() {

        addChildViewController(PGHomeTableViewController(), title: "首页", iconName: "tabbar_home")
        addChildViewController(PGMessageTableViewController(), title: "消息", iconName: "tabbar_message_center")
        //占位控制器
        addChildViewController(UIViewController())
        addChildViewController(PGDiscoverTableViewController(), title: "发现", iconName: "tabbar_discover")
        addChildViewController(PGProfileTableViewController(), title: "我", iconName: "tabbar_profile")
    }
    
    /// 创建自控制器
    ///
    /// - parameter Vc:       控制器
    /// - parameter title:    标题
    /// - parameter iconName: 图片名
    func addChildViewController(Vc: UIViewController, title: String, iconName: String) {
        Vc.title = title
        Vc.tabBarItem.image = UIImage(named: iconName)
        
        let nav = UINavigationController(rootViewController: Vc)
        self.addChildViewController(nav)
    }
}